﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Diagnostics;
using XYAC.Framework.MonitorTireMaterialManual.Entities;
using XYAC.Framework.MonitorTireMaterialManual.Extensions;
using XYAC.Framework.MonitorTireMaterialManual.LocalData.Models;
using XYAC.Framework.MonitorTireMaterialManual.OracleRemoteDB;

namespace XYAC.Framework.MonitorTireMaterialManual.LocalData.Context
{
    public class LocalDataContext : DbContext
    {
        public LocalDataContext(IOptionsMonitor<AppConfig> moduleConfig)
        {
            this.AppConfig = moduleConfig;
        }
        /// <summary>
        /// 用户表
        /// </summary>
        public DbSet<User> User { get; set; }
        /// <summary>
        /// 规格映射记录
        /// </summary>
        public DbSet<SpecInfoSyncMapping> SpecInfoSyncMapping { get; set; }
        /// <summary>
        /// 中间表,用于取MES条码数据
        /// </summary>
        public DbSet<YB_LOT_INSPECT_Local> YB_LOT_INSPECT { get; set; }
        /// <summary>
        /// 规格头表
        /// </summary>
        public DbSet<YB_Technology_Header> YB_Technology_Header { get; set; }
        /// <summary>
        /// 规格行表
        /// </summary>
        public DbSet<YB_Technology_Line> YB_Technology_Line { get; set; }
        /// <summary>
        /// 设备列表映射管理
        /// </summary>
        public DbSet<EquipmentMapping> EquipmentMapping { get; set; }

        public IOptionsMonitor<AppConfig> AppConfig { get; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<SpecInfoSyncMapping>().ToTable("SpecInfoSyncMapping").HasKey(h => h.ID);
            modelBuilder.Entity<YB_LOT_INSPECT_Local>(option =>
            {
                option.ToTable("YB_LOT_INSPECT");
                option.Property(h => h.LJ_ID).HasColumnName("LJ_ID");
            });

            modelBuilder.Entity<YB_Technology_Header>(option =>
            {
                option.ToTable("YB_TECHNOLOGY_HEADER");
                option.Property(h => h.LJ_ID).HasColumnName("LJ_ID");

            });
            modelBuilder.Entity<YB_Technology_Line>(option =>
            {
                option.ToTable("YB_TECHNOLOGY_LINE");
                option.Property(h => h.LJ_ID).HasColumnName("LJ_ID");

            });
            base.OnModelCreating(modelBuilder);
        }

        /// <summary>
        /// 当DB发生OnConfiguring用于配置数据库
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                // 配置 SQL Lite 数据库连接字符串
#if DEBUG
                //optionsBuilder.UseJet(AppConfig.CurrentValue?.LocalDataConnectionString);
                optionsBuilder.UseSqlite(AppConfig.CurrentValue?.LocalDataConnectionString).LogTo(DB, LogLevel.Debug);
#endif

#if RELEASE
                optionsBuilder.UseSqlite(AppConfig.CurrentValue?.LocalDataConnectionString);
#endif
            }
        }
        Action<string> DB => (string T) => { Debug.WriteLine(T); };
    }
}
